package com.golddaymicromall.activity.setting.personal;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.golddaymicromall.activity.R;
import com.golddaymicromall.application.MyApplication;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.Map;
import java.util.regex.Pattern;

import rx.android.schedulers.AndroidSchedulers;

public class SafeEditPwdActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout mEditPwdBack;
    private Button mSafeUserUpdatePwd;
    private EditText mOldUserPwd;
    private EditText mNewUserPwd;
    private EditText mConfirmNewUserPwd;
    private String customerNo;
    private String tel;
    private TextView mCustomerNo;
    private EditText mOldPayPwd;
    private EditText mNewPayPwd;
    private EditText mConfirmNewPayPwd;
    private Button mSafePayUpdatePwd;
    private LinearLayout mPayLinearlayout;
    private LinearLayout mLoginPwdBtn;
    private LinearLayout mTransactionPwdBtn;
    private LinearLayout mUpdatePwdChoice;
    private LinearLayout mLoginLayout;
    private TextView mLoginLine;
    private TextView mTransactionLine;
    private TextView mLoginText;
    private TextView mTransactionText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_edit_pwd);
        initView();
    }

    public void initView() {
        mEditPwdBack = (LinearLayout) findViewById(R.id.editpwd_back);
        mSafeUserUpdatePwd = (Button) findViewById(R.id.safe_UpdateUserPwd);
        mOldUserPwd = (EditText) findViewById(R.id.oldUserPwd);
        mNewUserPwd = (EditText) findViewById(R.id.newUserPwd);
        mConfirmNewUserPwd = (EditText) findViewById(R.id.confirmNewUserPwd);
        mCustomerNo = (TextView) findViewById(R.id.customerNO);
        mOldPayPwd = (EditText) findViewById(R.id.oldPayPwd);
        mNewPayPwd = (EditText) findViewById(R.id.newPayPwd);
        mConfirmNewPayPwd = (EditText) findViewById(R.id.confirmNewPayPwd);
        mSafePayUpdatePwd = (Button) findViewById(R.id.safe_updatePayPwd);
        mPayLinearlayout = (LinearLayout) findViewById(R.id.payLinearlayout);
        mLoginLayout = (LinearLayout) findViewById(R.id.loginLayout);
        mLoginPwdBtn = (LinearLayout) findViewById(R.id.loginpwdbtn);
        mTransactionPwdBtn = (LinearLayout) findViewById(R.id.transactionpwdbtn);
        mUpdatePwdChoice = (LinearLayout) findViewById(R.id.updatepwdchoice);
        mLoginLine = (TextView) findViewById(R.id.login_line);
        mTransactionLine = (TextView) findViewById(R.id.transaction_line);
        mLoginText = (TextView) findViewById(R.id.login_text);
        mTransactionText = (TextView) findViewById(R.id.transaction_text);
        mLoginPwdBtn.setOnClickListener(this);
        mTransactionPwdBtn.setOnClickListener(this);
        customerNo = SPUtils.get(getApplicationContext(), "Customerno", "").toString();
        tel = SPUtils.get(getApplicationContext(), "Tel", "").toString();
        if ("".equals(customerNo)) {
            mCustomerNo.setText(tel);
            mPayLinearlayout.setVisibility(View.GONE);
            mUpdatePwdChoice.setVisibility(View.GONE);
        } else {
            mCustomerNo.setText(customerNo);
        }
        mEditPwdBack.setOnClickListener(this);
        mSafeUserUpdatePwd.setOnClickListener(this);
        mSafePayUpdatePwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginpwdbtn:
                mLoginLayout.setVisibility(View.VISIBLE);
                mPayLinearlayout.setVisibility(View.GONE);
                mLoginText.setTextColor(getResources().getColor(R.color.pwd));
                mTransactionText.setTextColor(getResources().getColor(R.color.darkgray));
                mLoginLine.setBackgroundColor(getResources().getColor(R.color.pwd));
                mTransactionLine.setBackgroundColor(getResources().getColor(R.color.white));
                break;
            case R.id.transactionpwdbtn:
                mLoginLayout.setVisibility(View.GONE);
                mPayLinearlayout.setVisibility(View.VISIBLE);
                mLoginText.setTextColor(getResources().getColor(R.color.darkgray));
                mTransactionText.setTextColor(getResources().getColor(R.color.pwd));
                mLoginLine.setBackgroundColor(getResources().getColor(R.color.white));
                mTransactionLine.setBackgroundColor(getResources().getColor(R.color.pwd));
                break;
            case R.id.editpwd_back:
                finish();
                break;
            case R.id.safe_UpdateUserPwd:
                attemptUpdateUserTask();
                MyApplication.getInstance().attemptRiZhi(SPUtils.get(getApplicationContext(), "Customerno", "").toString(), SPUtils.get(getApplicationContext(), "Customerno", "").toString() + "修改登录密码");
                break;
            case R.id.safe_updatePayPwd:
                attmptUpdatePayTask();
                MyApplication.getInstance().attemptRiZhi(SPUtils.get(getApplicationContext(), "Customerno", "").toString(), SPUtils.get(getApplicationContext(), "Customerno", "").toString() + "修改支付密码");
                break;
        }
    }

    private void attemptUpdateUserTask() {

        String oldUserPwd = mOldUserPwd.getText().toString();
        String newUserPwd = mNewUserPwd.getText().toString();
        String confirmNewUserPwd = mConfirmNewUserPwd.getText().toString();

        if (newUserPwd.equals(confirmNewUserPwd)) {
            attemptUpdateUserPassword(customerNo, oldUserPwd, newUserPwd);
        } else {
            Toast.makeText(SafeEditPwdActivity.this, "两次登录密码不一致", Toast.LENGTH_SHORT).show();
        }
    }

    private void attemptUpdateUserPassword(String customerNo, String userOldPwd, String userNewPwd) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.user.updatepassword");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("password", userOldPwd);
        form.put("customerno", customerNo);
        form.put("newpassword", userNewPwd);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    if ("1".equals(s)) {
                        Toast.makeText(SafeEditPwdActivity.this, "修改登录密码成功,请重新登录", Toast.LENGTH_SHORT).show();
                        SPUtils.put(SafeEditPwdActivity.this, "Password", userNewPwd);
                        SPUtils.clear(getApplicationContext());
                        clearView();
                    } else {
                        Toast.makeText(SafeEditPwdActivity.this, "修改登录密码失败", Toast.LENGTH_SHORT).show();
                        clearView();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });


    }

    private static boolean isNum(String num) {
        String reg = "^([0-9a-fA-F]){2}([:][0-9a-fA-F]{2}){5}";
        return Pattern.compile(reg).matcher(num).find();
    }

    private void attmptUpdatePayTask() {
        String oldPayPwd = mOldPayPwd.getText().toString();
        String newPayPwd = mNewPayPwd.getText().toString();
        String confirmNewPayPwd = mConfirmNewPayPwd.getText().toString();
        if (newPayPwd.equals(confirmNewPayPwd)) {
            attmptUpdatePayTask(oldPayPwd, customerNo, newPayPwd);
        } else {
            Toast.makeText(SafeEditPwdActivity.this, "两次交易密码不一致", Toast.LENGTH_SHORT).show();
        }
    }
    private void attmptUpdatePayTask(String payOldPwd, String customerNo, String payNewPwd) {
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.user.updatepassword");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("trandword", payOldPwd);
        form.put("customerno", customerNo);
        form.put("newpassword", payNewPwd);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    if ("1".equals(s)) {
                        Toast.makeText(SafeEditPwdActivity.this, "修改交易密码成功", Toast.LENGTH_SHORT).show();
                        clearView();
                    } else {
                        Toast.makeText(SafeEditPwdActivity.this, "修改交易密码失败", Toast.LENGTH_SHORT).show();
                        clearView();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });
    }



    private void clearView() {
        mOldUserPwd.setText("");
        mNewUserPwd.setText("");
        mConfirmNewUserPwd.setText("");
        mOldPayPwd.setText("");
        mNewPayPwd.setText("");
        mConfirmNewPayPwd.setText("");
    }
}
